package com.whnk.model.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.whnk.common.core.web.controller.BaseController;
import com.whnk.common.core.web.domain.AjaxResult;
import com.whnk.model.domain.DatTIndexInfo;
import com.whnk.model.service.IDatTIndexInfoService;
import com.whnk.model.service.IDefTCustinfoService;
import com.whnk.model.vo.AreaInfoVo;
import com.whnk.model.vo.BigScreenQueryVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 大屏展示Controller
 * 模拟BI大屏展示报文服务
 * *  * 访问：URL：http://127.0.0.1:8090/engine/queryDataByEngine
 * *       json：{"txDate":"20211101","showPage":"1","areaId":"1"}
 *
 * @author xgq
 * @date
 */
@Api(tags = "大屏展示controller")
@RestController
@RequestMapping("/engine")
public class BigScreenViewController extends BaseController {


    @Autowired
    private IDatTIndexInfoService datTIndexInfoService;

    @GetMapping("/queryDataByEngine")
    public AjaxResult queryDataByEngine(BigScreenQueryVo bigScreenQueryVo) {
        String showPage = bigScreenQueryVo.getShowPage();
        String areaId = bigScreenQueryVo.getAreaId();
        DatTIndexInfo datTIndexInfo = new DatTIndexInfo();
        datTIndexInfo.setTimestring("20211101");
        datTIndexInfo.setBrCode("999999999");
        if ("1".equals(showPage) && "1".equals(areaId)) {
            datTIndexInfo.setIndexCode("Z_B_B_01");
            List<AreaInfoVo> areaInfoVos = new ArrayList<>();
            List<DatTIndexInfo> datTIndexInfos = datTIndexInfoService.selectDatTIndexInfoList1(datTIndexInfo);
            if (CollectionUtils.isNotEmpty(datTIndexInfos)) {
                // 进行拆解封装
                areaInfoVos = datTIndexInfos.stream().map(d -> {
                    AreaInfoVo areaInfoVo = new AreaInfoVo();
                    if (d.getBrName().equals("黑龙江省分行")) {
                        areaInfoVo.setName(d.getBrName().substring(0, 3));
                    } else {
                        areaInfoVo.setName(d.getBrName().substring(0, 2));
                    }
                    areaInfoVo.setValue(d.getIndexValue());
                    return areaInfoVo;
                }).collect(Collectors.toList());
            }
            return AjaxResult.success(areaInfoVos);
        } else if ("1".equals(showPage) && "2".equals(areaId)) {
            datTIndexInfo.setIndexCode("Z_B_B_01");
            List<DatTIndexInfo> datTIndexInfos = datTIndexInfoService.selectDatTIndexInfoList1(datTIndexInfo);
            List<String> collect1 = datTIndexInfos.stream().map(t -> t.getBrName()).collect(Collectors.toList());
            List<BigDecimal> collect2 = datTIndexInfos.stream().map(t -> t.getIndexValue()).collect(Collectors.toList());
            Map<String, List<?>> listHashMap = new HashMap<>();
            listHashMap.put("category", collect1);
            listHashMap.put("Direct", collect2);
            return AjaxResult.success(listHashMap);
        } else if ("1".equals(showPage) && "4".equals(areaId)) {
            List<AreaInfoVo> areaInfoVos = new ArrayList<>();
            datTIndexInfo.setIndexCode("Z_B_B_01");
            List<DatTIndexInfo> datTIndexInfos = datTIndexInfoService.selectDatTIndexInfoList2(datTIndexInfo);
            if (CollectionUtils.isNotEmpty(datTIndexInfos)) {
                // 进行拆解封装
                areaInfoVos = datTIndexInfos.stream().map(d -> {
                    AreaInfoVo areaInfoVo = new AreaInfoVo();
                    areaInfoVo.setName(d.getTtshastTplvCgynm());
                    areaInfoVo.setValue(d.getIndexValue());
                    return areaInfoVo;
                }).collect(Collectors.toList());
            }
            return AjaxResult.success(areaInfoVos);
        } else if ("2".equals(showPage) && "1".equals(areaId)) {
            datTIndexInfo.setTtshastTplvCgycd("Z_Q_Q");
            List<AreaInfoVo> areaInfos = datTIndexInfoService.selectAreaInfoList(datTIndexInfo);
            return AjaxResult.success(areaInfos);
        }else if ("2".equals(showPage) && "2".equals(areaId)) {
            datTIndexInfo.setTtshastTplvCgycd("Z_J_C");
            List<AreaInfoVo> areaInfos = datTIndexInfoService.selectAreaInfoList(datTIndexInfo);
            return AjaxResult.success(areaInfos);
        }else if ("2".equals(showPage) && "3".equals(areaId)) {
            datTIndexInfo.setTtshastTplvCgycd("Z_J_S");
            List<AreaInfoVo> areaInfos = datTIndexInfoService.selectAreaInfoList(datTIndexInfo);
            List<HashMap> list = new ArrayList<>();
            HashMap<String, String> linkedHashMap = new LinkedHashMap<>();
            linkedHashMap.put("name",areaInfos.get(0).getName());
            linkedHashMap.put("value",areaInfos.get(0).getValue().stripTrailingZeros().toPlainString()+"万人");
            HashMap<String, String> linkedHashMap1 = new LinkedHashMap<>();
            linkedHashMap1.put("name",areaInfos.get(1).getName());
            linkedHashMap1.put("value",areaInfos.get(1).getValue().stripTrailingZeros().toPlainString()+"万笔");
            HashMap<String, String> linkedHashMap2 = new LinkedHashMap<>();
            linkedHashMap2.put("name",areaInfos.get(2).getName());
            linkedHashMap2.put("value",areaInfos.get(2).getValue().stripTrailingZeros().toPlainString()+"亿美元");
            list.add(linkedHashMap);
            list.add(linkedHashMap1);
            list.add(linkedHashMap2);
            return AjaxResult.success(list);
        }else if ("2".equals(showPage) && "4".equals(areaId)) {
            datTIndexInfo.setTtshastTplvCgycd("Z_J_R");
            List<AreaInfoVo> areaInfos = datTIndexInfoService.selectAreaInfoList(datTIndexInfo);
            List<HashMap> list = new ArrayList<>();
            HashMap<String, String> linkedHashMap = new LinkedHashMap<>();
            linkedHashMap.put("name",areaInfos.get(0).getName());
            linkedHashMap.put("value",areaInfos.get(0).getValue().stripTrailingZeros().toPlainString()+"万人");
            HashMap<String, String> linkedHashMap1 = new LinkedHashMap<>();
            linkedHashMap1.put("name",areaInfos.get(1).getName());
            linkedHashMap1.put("value",areaInfos.get(1).getValue().stripTrailingZeros().toPlainString()+"万人");
            HashMap<String, String> linkedHashMap2 = new LinkedHashMap<>();
            linkedHashMap2.put("name",areaInfos.get(2).getName());
            linkedHashMap2.put("value",areaInfos.get(2).getValue().stripTrailingZeros().toPlainString()+"万笔");
            HashMap<String, String> linkedHashMap3 = new LinkedHashMap<>();
            linkedHashMap3.put("name",areaInfos.get(3).getName());
            linkedHashMap3.put("value",areaInfos.get(3).getValue().stripTrailingZeros().toPlainString()+"万笔");
            HashMap<String, String> linkedHashMap4 = new LinkedHashMap<>();
            linkedHashMap4.put("name",areaInfos.get(4).getName());
            linkedHashMap4.put("value",areaInfos.get(4).getValue().stripTrailingZeros().toPlainString()+"%");
            list.add(linkedHashMap);
            list.add(linkedHashMap1);
            list.add(linkedHashMap2);
            list.add(linkedHashMap3);
            list.add(linkedHashMap4);
            return AjaxResult.success(list);
        }else if ("2".equals(showPage) && "5".equals(areaId)) {
            datTIndexInfo.setTtshastTplvCgycd("Z_X_S");
            List<AreaInfoVo> areaInfos = datTIndexInfoService.selectAreaInfoList(datTIndexInfo);
            return AjaxResult.success(areaInfos);
        }else if ("2".equals(showPage) && "6".equals(areaId)) {
            datTIndexInfo.setTtshastTplvCgycd("Z_X_H");
            List<AreaInfoVo> areaInfos = datTIndexInfoService.selectAreaInfoList(datTIndexInfo);
            List<HashMap> list = new ArrayList<>();
            HashMap<String, String> linkedHashMap = new LinkedHashMap<>();
            linkedHashMap.put("name",areaInfos.get(0).getName());
            linkedHashMap.put("value",areaInfos.get(0).getValue().stripTrailingZeros().toPlainString()+"人");
            HashMap<String, String> linkedHashMap1 = new LinkedHashMap<>();
            linkedHashMap1.put("name",areaInfos.get(1).getName());
            linkedHashMap1.put("value",areaInfos.get(1).getValue().stripTrailingZeros().toPlainString()+"笔");
            HashMap<String, String> linkedHashMap2 = new LinkedHashMap<>();
            linkedHashMap2.put("name",areaInfos.get(2).getName());
            linkedHashMap2.put("value",areaInfos.get(2).getValue().stripTrailingZeros().toPlainString()+"元");
            HashMap<String, String> linkedHashMap3 = new LinkedHashMap<>();
            linkedHashMap3.put("name",areaInfos.get(3).getName());
            linkedHashMap3.put("value",areaInfos.get(3).getValue().stripTrailingZeros().toPlainString()+"笔");
            HashMap<String, String> linkedHashMap4 = new LinkedHashMap<>();
            linkedHashMap4.put("name",areaInfos.get(4).getName());
            linkedHashMap4.put("value",areaInfos.get(4).getValue().stripTrailingZeros().toPlainString()+"笔");
            list.add(linkedHashMap);
            list.add(linkedHashMap1);
            list.add(linkedHashMap2);
            list.add(linkedHashMap3);
            list.add(linkedHashMap4);
            return AjaxResult.success(list);
        }else if ("2".equals(showPage) && "7".equals(areaId)) {
            datTIndexInfo.setTtshastTplvCgycd("Z_J_K");
            List<AreaInfoVo> areaInfos = datTIndexInfoService.selectAreaInfoList(datTIndexInfo);
            List<HashMap> list = new ArrayList<>();
            HashMap<String, String> linkedHashMap = new LinkedHashMap<>();
            linkedHashMap.put("name",areaInfos.get(0).getName());
            linkedHashMap.put("value",areaInfos.get(0).getValue().stripTrailingZeros().toPlainString()+"人");
            HashMap<String, String> linkedHashMap1 = new LinkedHashMap<>();
            linkedHashMap1.put("name",areaInfos.get(1).getName());
            linkedHashMap1.put("value",areaInfos.get(1).getValue().stripTrailingZeros().toPlainString()+"亿元");
            HashMap<String, String> linkedHashMap2 = new LinkedHashMap<>();
            linkedHashMap2.put("name",areaInfos.get(2).getName());
            linkedHashMap2.put("value",areaInfos.get(2).getValue().stripTrailingZeros().toPlainString()+"吨");
            HashMap<String, String> linkedHashMap3 = new LinkedHashMap<>();
            linkedHashMap3.put("name",areaInfos.get(3).getName());
            linkedHashMap3.put("value",areaInfos.get(3).getValue().stripTrailingZeros().toPlainString()+"个");
            list.add(linkedHashMap);
            list.add(linkedHashMap1);
            list.add(linkedHashMap2);
            list.add(linkedHashMap3);
            return AjaxResult.success(list);
        }else if ("2".equals(showPage) && "8".equals(areaId)) {
            datTIndexInfo.setTtshastTplvCgycd("Z_X_C");
            List<AreaInfoVo> areaInfos = datTIndexInfoService.selectAreaInfoList(datTIndexInfo);
            List<HashMap> list = new ArrayList<>();
            HashMap<String, String> linkedHashMap = new LinkedHashMap<>();
            linkedHashMap.put("name",areaInfos.get(0).getName());
            linkedHashMap.put("value",areaInfos.get(0).getValue().stripTrailingZeros().toPlainString());
            linkedHashMap.put("value1","万笔");
            HashMap<String, String> linkedHashMap1 = new LinkedHashMap<>();
            linkedHashMap1.put("name",areaInfos.get(1).getName());
            linkedHashMap1.put("value",areaInfos.get(1).getValue().stripTrailingZeros().toPlainString());
            linkedHashMap1.put("value1","亿元");
            HashMap<String, String> linkedHashMap2 = new LinkedHashMap<>();
            linkedHashMap2.put("name",areaInfos.get(2).getName());
            linkedHashMap2.put("value",areaInfos.get(2).getValue().stripTrailingZeros().toPlainString());
            linkedHashMap2.put("value1","万笔");
            HashMap<String, String> linkedHashMap3 = new LinkedHashMap<>();
            linkedHashMap3.put("name",areaInfos.get(3).getName());
            linkedHashMap3.put("value",areaInfos.get(3).getValue().stripTrailingZeros().toPlainString());
            linkedHashMap3.put("value1","亿元");
            HashMap<String, String> linkedHashMap4 = new LinkedHashMap<>();
            linkedHashMap4.put("name",areaInfos.get(4).getName());
            linkedHashMap4.put("value",areaInfos.get(4).getValue().stripTrailingZeros().toPlainString());
            linkedHashMap4.put("value1","万笔");
            HashMap<String, String> linkedHashMap5 = new LinkedHashMap<>();
            linkedHashMap5.put("name",areaInfos.get(5).getName());
            linkedHashMap5.put("value",areaInfos.get(5).getValue().stripTrailingZeros().toPlainString());
            linkedHashMap5.put("value1","亿元");
            HashMap<String, String> linkedHashMap6 = new LinkedHashMap<>();
            linkedHashMap6.put("name",areaInfos.get(6).getName());
            linkedHashMap6.put("value",areaInfos.get(6).getValue().stripTrailingZeros().toPlainString());
            linkedHashMap6.put("value1","万笔");
            list.add(linkedHashMap);
            list.add(linkedHashMap1);
            list.add(linkedHashMap2);
            list.add(linkedHashMap3);
            list.add(linkedHashMap4);
            list.add(linkedHashMap5);
            list.add(linkedHashMap6);
            return AjaxResult.success(list);
        }else if ("2".equals(showPage) && "9".equals(areaId)) {
            datTIndexInfo.setTtshastTplvCgycd("Z_Y_Z");
            List<AreaInfoVo> areaInfos = datTIndexInfoService.selectAreaInfoList(datTIndexInfo);
            return AjaxResult.success(areaInfos);
        } else if ("3".equals(showPage) && "1".equals(areaId)) {
            datTIndexInfo.setTtshastTplvCgycd("Z_A_F");
            List<AreaInfoVo> areaInfos = datTIndexInfoService.selectAreaInfoList(datTIndexInfo);
            List<HashMap> list = new ArrayList<>();
            HashMap<String, String> linkedHashMap = new LinkedHashMap<>();
            linkedHashMap.put("name",areaInfos.get(0).getName());
            linkedHashMap.put("value",areaInfos.get(0).getValue().stripTrailingZeros().toPlainString()+"笔");
            HashMap<String, String> linkedHashMap1 = new LinkedHashMap<>();
            linkedHashMap1.put("name",areaInfos.get(1).getName());
            linkedHashMap1.put("value",areaInfos.get(1).getValue().stripTrailingZeros().toPlainString()+"笔");
            HashMap<String, String> linkedHashMap2 = new LinkedHashMap<>();
            linkedHashMap2.put("name",areaInfos.get(2).getName());
            linkedHashMap2.put("value",areaInfos.get(2).getValue().stripTrailingZeros().toPlainString()+"笔");
            HashMap<String, String> linkedHashMap3 = new LinkedHashMap<>();
            linkedHashMap3.put("name",areaInfos.get(3).getName());
            linkedHashMap3.put("value",areaInfos.get(3).getValue().stripTrailingZeros().toPlainString()+"%");
            list.add(linkedHashMap);
            list.add(linkedHashMap1);
            list.add(linkedHashMap2);
            list.add(linkedHashMap3);
            return AjaxResult.success(list);

        } else if ("3".equals(showPage) && "2".equals(areaId)) {
            datTIndexInfo.setTtshastTplvCgycd("Z_Z_C");
            List<AreaInfoVo> areaInfos = datTIndexInfoService.selectAreaInfoList(datTIndexInfo);
            areaInfos.sort(Comparator.comparing(AreaInfoVo::getValue));
            AreaInfoVo areaInfoVo = new AreaInfoVo();
            areaInfoVo.setName("稽核问题总笔数");
            BigDecimal total = new BigDecimal("0");
            for (AreaInfoVo areaInfo : areaInfos) {
                total=total.add(areaInfo.getValue());
            }
            areaInfoVo.setValue(total);
            ArrayList<AreaInfoVo> list = new ArrayList<>();
            list.add(areaInfoVo);
            for (int i = 0; i <areaInfos.size(); i++) {
                list.add(areaInfos.get(i));
            }
            List<Object> objects = new ArrayList<>();
            objects.add(areaInfoVo);
            objects.add(areaInfos);
            return AjaxResult.success(objects);
        }else if ("3".equals(showPage) && "3".equals(areaId)) {
            DatTIndexInfo datTIndexInfo1 = new DatTIndexInfo();
            datTIndexInfo1.setTimestring("20211101");
            datTIndexInfo1.setIndexCode("Z_S_J_01");
            List<DatTIndexInfo> datTIndexInfos = datTIndexInfoService.selectDatTIndexInfoList(datTIndexInfo1);
            datTIndexInfos.sort(Comparator.comparing(DatTIndexInfo::getIndexValue).reversed());
            List<AreaInfoVo> areaInfoVos = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(datTIndexInfos)) {
                // 进行拆解封装
                areaInfoVos = datTIndexInfos.stream().map(d -> {
                    AreaInfoVo areaInfoVo = new AreaInfoVo();
                    areaInfoVo.setName(d.getBrName());
                    areaInfoVo.setValue(d.getIndexValue());
                    return areaInfoVo;
                }).collect(Collectors.toList());
            }
            return AjaxResult.success(areaInfoVos);
        }
        return null;
    }
}
